/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.render.exporter;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopyFields;
import com.lowagie.text.pdf.PdfReader;
import org.retabel.persistence.entity.document.ContentTypeEnum;
import org.retabel.persistence.entity.render.RenderedDocumentPart;
import org.retabel.service.render.RenderServiceException;
import org.retabel.service.render.exportable.ExportableDocumentPart;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * Interface to be implemented by rendering supported rendering engines
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 11:28 15.10.11
 *
 * @author Peter Herber
 * @version 1.0
 */
public abstract class RenderingEngine {

    public abstract Set<ContentTypeEnum> supportedContentTypes();

    public abstract Set<ContentTypeEnum> supportsConcatenationForContentTypes();

    public abstract RenderedDocumentPart exportAndConcatenate(List<? extends ExportableDocumentPart> exportableDocumentParts,
                                                              ContentTypeEnum contentType) throws RenderServiceException;

    public boolean supportsContentType(ContentTypeEnum contentType) {
        return supportedContentTypes().contains(contentType);
    }

    public boolean supportsConcatenationForContentType(ContentTypeEnum contentType) {
        return supportsConcatenationForContentTypes().contains(contentType);
    }

    // utility functions

    public static byte[] concatPDFs(List<byte[]> pdfs) throws IOException, DocumentException {

        if (pdfs == null || pdfs.size() == 0) {
            return null;
        }

        if (pdfs.size() == 1) {
            return pdfs.get(0);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfCopyFields copy = new PdfCopyFields(baos);

        for (byte[] pdf : pdfs) {
            PdfReader reader = new PdfReader(pdf);

            copy.addDocument(reader);
        }
        copy.close();

        return baos.toByteArray();
    }
}
